package com.liao.member.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liao.common.constant.SessionEnum;
import com.liao.common.entity.member.UserAddressEntity;
import com.liao.common.entity.member.UserEntity;
import com.liao.common.utils.PageUtils;
import com.liao.member.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 用户收货地址簿
 *
 * @author liao
 * @email 2858015963@qq.com
 * @date 2025-03-10 19:27:53
 */
@RestController
@RequestMapping("member/useraddress")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    /**
     * @ignore 列表
     */

    @RequestMapping("/list")
    public PageUtils<UserAddressEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils page = userAddressService.queryPage(params);

        return page;
    }


    /**
     * @ignore 信息
     */
    @RequestMapping("/info/{addressId}")
    public UserAddressEntity addressInfo(@PathVariable("addressId") Integer addressId) {
        return userAddressService.getById(addressId);
    }

    /**
     * @ignore 保存
     */
    @RequestMapping("/save")
    public void save(@RequestBody UserAddressEntity userAddress) {
        userAddressService.save(userAddress);

    }

    /**
     * @ignore 修改
     */
    @RequestMapping("/update")
    public void update(@RequestBody UserAddressEntity userAddress) {
        userAddressService.updateById(userAddress);
    }

    /**
     * @ignore 删除
     */
    @RequestMapping("/delete")
    public void delete(@RequestBody Integer[] addressIds) {
        userAddressService.removeByIds(Arrays.asList(addressIds));
    }

    /**
     * 获取用户收货地址
     *
     * @return
     * @RequestParam Integer id 用户id
     */
    @GetMapping("/getAddress/{id}")
    public List<UserAddressEntity> getAddress(@PathVariable("id") Integer id) {
        List<UserAddressEntity> list = userAddressService.list(new QueryWrapper<UserAddressEntity>().eq("user_id", id));
        return list;
    }

    /**
     * 添加收货地址
     *
     * @param userAddressEntity
     * @apiNote UserAddressEntity必填
     */
    @PostMapping("/saveAddress")
    public void saveAddress(@RequestBody UserAddressEntity userAddressEntity) {
        if (StpUtil.isLogin()) {
            UserEntity user = (UserEntity) StpUtil.getSession().get(SessionEnum.USER_INFO_KEY);
            userAddressEntity.setUserId(user.getUserId());
        }
        userAddressEntity.setAddressId(null);
//        userAddressEntity.setUserId((Integer) StpUtil.getLoginId());
        boolean save = userAddressService.save(userAddressEntity);

        /**
         * 修改其他地址的默认地址
         */
        if (userAddressEntity.getIsDefault() == 1) {
            List<UserAddressEntity> list = userAddressService.list(new QueryWrapper<UserAddressEntity>().eq("user_id", userAddressEntity.getUserId()));

            for (UserAddressEntity addressEntity : list) {
                if (addressEntity.getAddressId() != userAddressEntity.getAddressId()) {
                    addressEntity.setIsDefault(0);
                    userAddressService.updateById(addressEntity);
                }
            }

        }
    }

    /**
     * 删除收货地址
     *
     * @param id 收货地址id
     */
    @PostMapping("/deleteAddress/{id}")
    public void deleteAddress(@PathVariable("id") String id) {
        UserAddressEntity byId = userAddressService.getById(id);
        if (byId.getIsDefault() == 1) {
            throw new RuntimeException("默认地址不能删除");
        }
        userAddressService.removeById(id);
    }

    @RequestMapping("/updateAddress/{id}")
    public void updateAddress(@PathVariable("id") Integer id, @RequestBody UserAddressEntity userAddress) {


        userAddress.setAddressId(id);
        userAddressService.updateById(userAddress);

        Integer isDefault = userAddress.getIsDefault();

        /**
         * 修改其他地址的默认地址
         */
        if (isDefault == 1) {
            List<UserAddressEntity> list = userAddressService.list(new QueryWrapper<UserAddressEntity>().eq("user_id", userAddress.getUserId()));

            for (UserAddressEntity addressEntity : list) {
                if (addressEntity.getAddressId() != id) {
                    addressEntity.setIsDefault(0);
                    userAddressService.updateById(addressEntity);
                }
            }

        }
    }

    @RequestMapping("/addressList")
    public List<UserAddressEntity> addressList() {
        if (!StpUtil.isLogin()) {
            throw new RuntimeException("请先登录");
        }
        UserEntity user = (UserEntity) StpUtil.getSession().get(SessionEnum.USER_INFO_KEY);

        QueryWrapper<UserAddressEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id", user.getUserId());

        List<UserAddressEntity> list = userAddressService.list(queryWrapper);
        return list;

    }


}
